package cn.szabadsag.hrm.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yangcong
 * @date 2022年04月22日 22:15
 * @since
 */
@Configuration
public class RabbitMQConfig {

    //交换机名称
    public static final String NAME_EXCHANGE_TOPIC_KILL = "NAME_EXCHANGE_TOPIC_KILL";

    //延迟队列名称
    public static final String NAME_QUEUE_ORDER_DELAY = "NAME_QUEUE_ORDER_DELAY";

    //死信队列名称
    public static final String NAME_QUEUE_ORDER_DEAD = "NAME_QUEUE_ORDER_DEAD";

    //延迟队列得routingkey
    public static final String ROUTINGKEY_ORDER_DELAY = "order.delay";

    //死信队列得routingkey
    public static final String ROUTINGKEY_ORDER_DEAD = "order.dead";

    /**
     * 交换机
     * Exchange选择时有core就用core
     */
    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(NAME_EXCHANGE_TOPIC_KILL).durable(true).build();
    }

    /**
     * 订单延迟队列
     */
    @Bean
    public Queue delayQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", NAME_EXCHANGE_TOPIC_KILL);    //过期的消息给哪个交换机的名字
        map.put("x-dead-letter-routing-key", ROUTINGKEY_ORDER_DEAD);   //死信交换机把消息个哪个个routingkey
        map.put("x-message-ttl", 10000);    //队列过期时间10s
        return new Queue(NAME_QUEUE_ORDER_DELAY,true,false,false,map);
    }

    /**
     * 死信队列
     */
    @Bean
    public Queue deadQueue(){
        return new Queue(NAME_QUEUE_ORDER_DEAD,true);
    }

    /**
     * 绑定延迟队列
     */
    @Bean
    public Binding bindDelayQueue(){
        return BindingBuilder.bind(delayQueue()).to(exchange()).with(ROUTINGKEY_ORDER_DELAY).noargs();
    }

    /**
     * 绑定死信队列
     */
    @Bean
    public Binding bindDeadQueue(){
        return BindingBuilder.bind(deadQueue()).to(exchange()).with(ROUTINGKEY_ORDER_DEAD).noargs();
    }






























}
